package com.ccc.influxdb.util;

import cn.hutool.core.util.StrUtil;
import com.ccc.influxdb.config.InfluxDBProperties;
import com.influxdb.client.BucketsApi;
import com.influxdb.client.BucketsQuery;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.OrganizationsQuery;
import com.influxdb.client.domain.Authorization;
import com.influxdb.client.domain.Bucket;
import com.influxdb.client.domain.BucketRetentionRules;
import com.influxdb.client.domain.Organization;
import com.influxdb.client.domain.Permission;
import com.influxdb.client.domain.PermissionResource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * InfluxDB操作平台工具类
 *
 * @author superC
 * @version 2.0.0
 * @date 2023/3/3
 */

@Component
@Slf4j
public class InfluxPlatformUtil {

    @Resource(name = "platformClient")
    private InfluxDBClient platformClient;

    @Autowired
    private InfluxDBProperties properties;

    public List<Organization> getOrgs() {
        return platformClient.getOrganizationsApi().findOrganizations();
    }

    public List<Organization> getOrgs(OrganizationsQuery query) {
        return platformClient.getOrganizationsApi().findOrganizations(query);
    }

    public Organization createOrg(String orgName) {
        return platformClient.getOrganizationsApi()
                .createOrganization(orgName);
    }

    public void deleteOrg(String orgId) {
        platformClient.getOrganizationsApi().deleteOrganization(orgId);
        log.info("删除org完毕！");
    }

    public List<Bucket> getBuckets(String orgName) {
        return platformClient.getBucketsApi().findBucketsByOrgName(orgName);
    }

    public List<Bucket> getBuckets(BucketsQuery query) {
        return platformClient.getBucketsApi().findBuckets(query);
    }

    /**
     * @param orgId       ORG ID
     * @param bucketName  bucket名称
     * @param expireValue 存储策略，用于设置数据保留的时间，单位天；若传入null值则设置为永久；
     * @return
     */
    public Bucket createBucket(String orgId, String bucketName, Integer expireValue) {
        if (expireValue != null) {
            // RR 指定保留规则
            BucketRetentionRules bucketRetentionRules = new BucketRetentionRules();
            bucketRetentionRules.setEverySeconds(expireValue * 86400);
            return platformClient.getBucketsApi().createBucket(bucketName, bucketRetentionRules, orgId);
        }
        // 默认Retention: Forever
        return platformClient.getBucketsApi().createBucket(bucketName, orgId);
    }

    public void deleteBucket(String bucketName) {
        BucketsApi bucketsApi = platformClient.getBucketsApi();
        Bucket bucket = bucketsApi.findBucketByName(bucketName);
        if (bucket != null && StrUtil.isNotEmpty(bucket.getId())) {
            bucketsApi.deleteBucket(bucket);
            log.info("删除bucket完毕！");
        } else {
            log.error("当前{}不存在！", bucketName);
        }
    }

    public Authorization createAuthorization(String orgId) {
        //
        // Add Permissions to read and write to the Org
        //
        PermissionResource resource = new PermissionResource();
        resource.setOrgID(orgId);
        resource.setType(PermissionResource.TYPE_ORGS);

        Permission readBucket = new Permission();
        readBucket.setResource(resource);
        readBucket.setAction(Permission.ActionEnum.READ);

        Permission writeBucket = new Permission();
        writeBucket.setResource(resource);
        writeBucket.setAction(Permission.ActionEnum.WRITE);

        return platformClient.getAuthorizationsApi()
                .createAuthorization(orgId, Arrays.asList(readBucket, writeBucket));

    }

    public Authorization createAuthorization2(String orgId, String bucketId) {
        //
        // Add Permissions to read and write to the Bucket
        //
        PermissionResource resource = new PermissionResource();
        resource.setId(bucketId);
        resource.setOrgID(orgId);
        resource.setType(PermissionResource.TYPE_ORGS);

        Permission readBucket = new Permission();
        readBucket.setResource(resource);
        readBucket.setAction(Permission.ActionEnum.READ);

        Permission writeBucket = new Permission();
        writeBucket.setResource(resource);
        writeBucket.setAction(Permission.ActionEnum.WRITE);

        return platformClient.getAuthorizationsApi()
                .createAuthorization(orgId, Arrays.asList(readBucket, writeBucket));

    }

}